// src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import {getValue} from "@/utils/cookie";

const push = Router.prototype.push
const replace = Router.prototype.replace

Router.prototype.push = function (location) {
    return push.call(this,location).catch(err => err)
}

Router.prototype.replace = function (location) {
    return replace.call(this,location).catch(err => err)
}

Vue.use(Router)


/* 路由懒加载 */
const Login   = () => import('@/views/Login.vue')
const Index   = () => import('@/views/Index.vue')
const VideoMeeting = () => import('@/components/VideoMeeting.vue')

const router = new Router({
    // mode: 'history',          // 去掉 #
    mode: 'hash',
    routes: [
        {
            path: '/',
            redirect: '/login',
        },
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: { title: 'Real Vision | 登录', auth: false },
        },
        {
            path: '/index',
            name: 'Index',
            component: Index,
            meta: { title: 'Real Vision | Welcome', auth: false },
        },
        {
            path: '/videoMeeting',
            name: 'VideoMeeting',
            component: VideoMeeting,
            meta: { title: 'Real Vision | 视频会议', auth: true },
        },
    ]
})

/* 全局前置守卫 */
router.beforeEach((to, from, next) => {
    // 1. 动态标题
    document.title = to.meta.title || 'Real Vision'
    // 2. 登录鉴权
    if (to.meta.auth && !getValue('meta-token')) {
        next('/login')
    } else {
        next()
    }
})

export default router